package com.chen.common;

import com.chen.entity.R;
import com.chen.exception.DeleteException;
import com.chen.exception.NameDuplicateException;
import com.chen.exception.StatusException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/*
 *
 * 全局异常捕获*/
@ControllerAdvice(annotations = {Service.class, RestController.class})
@ResponseBody
public class ExceptionHandler1 {
    @ExceptionHandler(NameDuplicateException.class)
    public R exceptionHandler(NameDuplicateException ex) {
        return R.error("该账号已存在");
    }

    @ExceptionHandler(DeleteException.class)
    public R deleteException(DeleteException ex) {
        return R.error("该分类还有相关菜品或套餐,无法删除");
    }
    @ExceptionHandler(StatusException.class)
    public R StatusException(StatusException ex) {
        return R.error(ex.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public R exception(NameDuplicateException ex) {
        return R.error("未知异常");
    }
}
